#include "stdafx.h"
#include "cCommander.h"
#include "common/commonDefFun.h"

#ifdef _WIN32
#include "libHvDevice_win/HvDeviceNew.h"
#include "libHvDevice_win/HvDeviceCommDef.h"
#include "libHvDevice_win/HvCamera.h"
#else
#include <pthread.h>
#include "libHvDevice_linux/HvDeviceCommDef.h"
#include "libHvDevice_linux/HvDeviceEx.h"
#endif // WIN32

#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <time.h>


using namespace libtool_sdk;

cBaseCommander::cBaseCommander() :
    m_szReturnString(NULL)
{
}

cBaseCommander::~cBaseCommander()
{
    SAFE_DELETE_ARRAY(m_szReturnString);
}

const char* cBaseCommander::GetLastExecReturnString()
{
    return m_szReturnString;
}

cCaptureCommander::cCaptureCommander(): cBaseCommander()
    , m_iLaneID(-1)
    , m_iCapID(-1)
{
}

int cCaptureCommander::Execute(void* handle)
{
    if (handle == NULL)
        return -1;

    int iRet = -1;
    char chRetBuf[1024] = { 0 };
    int nRetLen = 0;

    char chCMD[256] = { 0 };
    if (m_iCapID != -1 && m_iLaneID != -1)
    {
        sprintf(chCMD, "ForceTrigger,ID[%d],LANE[%d]", m_iCapID, m_iLaneID);
    }
    else
    {
        sprintf(chCMD, "SoftTriggerCapture");
    }

    if (HVAPI_ExecCmdEx(handle, chCMD, chRetBuf, sizeof(chRetBuf), &nRetLen) != S_OK)
    {
        iRet = 2;
    }
    else
    {
        iRet = 0;
        SAFE_DELETE_ARRAY(m_szReturnString);
        m_szReturnString = new char[nRetLen + 1];
        if (m_szReturnString != NULL)
        {
            memcpy(m_szReturnString, chRetBuf, nRetLen);
            m_szReturnString[nRetLen] = '\0';
        }
    }
    return iRet;
}

void cCaptureCommander::SetCaptureParam(int iLaneID, int iCapID)
{
    m_iLaneID = iLaneID;
    m_iCapID = iCapID;
}

cOverlayCommander::cOverlayCommander(): cBaseCommander()
    , m_iChannelID(0)
    , m_szOverlayString(NULL)   
{
}

cOverlayCommander::~cOverlayCommander()
{
    SAFE_DELETE_ARRAY(m_szOverlayString);
}

int cOverlayCommander::Execute(void* handle)
{
    if (handle == NULL || m_szOverlayString == NULL)
        return -1;

    if(0 == HVAPI_SetOSDText(handle, m_iChannelID, const_cast<char*>(m_szOverlayString)))
    {
        return 0;
    }
    else
    {
        return -2;
    }
}

void cOverlayCommander::SetOverlayParam(int iChannelID, const char* szOverlayText, int iTextLen)
{
    m_iChannelID = iChannelID;
    SAFE_DELETE_ARRAY(m_szOverlayString);
    m_szOverlayString = new char[iTextLen + 1];
    if (m_szOverlayString != NULL)
    {
        memcpy(m_szOverlayString, szOverlayText, iTextLen);
        m_szOverlayString[iTextLen] = '\0';
    }
}

cSetTimeCommander::cSetTimeCommander(): cBaseCommander()
    , m_iYear(0)
    , m_iMonth(0)
    , m_iDay(0)
    , m_iHour(0)
    , m_iMinute(0)
    , m_iSecond(0)
    , m_iMilientSecond(0)
{
}

int cSetTimeCommander::Execute(void* handle)
{
    if (handle == NULL)
        return -1;

    int iRet = -1;
    char chRetBuf[1024] = { 0 };
    int nRetLen = 0;

    char chCMD[256] = { 0 };
    if (m_iYear == 0)
    {
        time_t timeNow = time(NULL);
        struct tm* pTime = localtime(&timeNow);
        sprintf(chCMD, "SetTime,Date[%d.%02d.%02d],Time[%02d:%02d:%02d %03d]", 
            pTime->tm_year + 1900, 
            pTime->tm_mon + 1, 
            pTime->tm_mday, 
            pTime->tm_hour, 
            pTime->tm_min, 
            pTime->tm_sec, 
            0);
    }
    else
    {
        sprintf(chCMD, "SetTime,Date[%d.%02d.%02d],Time[%02d:%02d:%02d %03d]",
            m_iYear,
            m_iMonth,
            m_iDay,
            m_iHour,
            m_iMinute,
            m_iSecond,
            m_iMilientSecond);
    }

    if (HVAPI_ExecCmdEx(handle, chCMD, chRetBuf, sizeof(chRetBuf), &nRetLen) != S_OK)
    {
        iRet = 2;
    }
    else
    {
        iRet = 0;
        SAFE_DELETE_ARRAY(m_szReturnString);
        m_szReturnString = new char[nRetLen + 1];
        if (m_szReturnString != NULL)
        {
            memcpy(m_szReturnString, chRetBuf, nRetLen);
            m_szReturnString[nRetLen] = '\0';
        }
    }
    return iRet;
}

void cSetTimeCommander::SetTimeParam(int Year, int Month, int Day, int Hour, int Minute, int Second, int MilientSecond)
{
    m_iYear = Year;
    m_iMonth = Month;
    m_iDay = Day;
    m_iHour = Hour;
    m_iMinute = Minute;
    m_iSecond = Second;
    m_iMilientSecond = MilientSecond;
}
